package com.edu.controller;


import com.edu.bean.User;
import com.edu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/userRegister")
    public ModelAndView userRegister(User user){
       int result = userService.userRegister(user);
       ModelAndView mv = new ModelAndView();
       if(result == -1){
            mv.addObject("msg","用户名重复");
            mv.setViewName("register.jsp");
       }else if(result == 0){
           mv.addObject("msg","注册失败，请稍后再试");
           mv.setViewName("register.jsp");
       }else{
           mv.setViewName("success.jsp");
       }
       return mv;
    }


    @RequestMapping("/userLogin")
    public ModelAndView userLogin(User user){
        int result = userService.userLogin(user);
        ModelAndView mv = new ModelAndView();
        if(result == -1){
            mv.addObject("msg","用户名不存在");
            mv.setViewName("login.jsp");
        }else if(result == 0){
            mv.addObject("msg","密码错误");
            mv.setViewName("login.jsp");
        }else if(result == 1){
            mv.setViewName("success.jsp");
        }
        return mv;
    }

}
